package com.blog.controller;


import com.blog.common.Result.PageResult;
import com.blog.common.Result.Result;
import com.blog.dto.ArticleAddDto;
import com.blog.dto.ArticleUpdateDto;
import com.blog.entity.Article;
import com.blog.service.IArticleService;
import com.blog.vo.ArticleGetByDateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/article")
@Api(tags = "文章相关接口")
@Slf4j
@RequiredArgsConstructor
public class ArticleController {
    private final IArticleService articleService;

    /**
     * 添加文章
     * @param articleAddDto
     * @return
     */
    @PostMapping("/addArticle")
    @ApiOperation(value = "添加文章")
    @CacheEvict(cacheNames = "articleListByDate",allEntries = true)
    public Result<String> addArticle(@RequestBody ArticleAddDto articleAddDto) {
        log.info("添加文章：{}",articleAddDto);
        articleService.addArticle(articleAddDto);
        return Result.success("添加文章成功");
    }

    /**
     * 根据更新时间获取文章列表
     * @return
     */
    @GetMapping("/getListByDate")
    @ApiOperation(value = "根据更新时间获取文章列表")
    @Cacheable(cacheNames = "articleListByDate")
    public Result<List<ArticleGetByDateVo>> getListByDate() {
        log.info("根据更新时间获取文章列表");
        List<ArticleGetByDateVo> listByDate = articleService.getListByDate();
        return Result.success(listByDate);
    }


    @DeleteMapping("/deleteArticle/{id}")
    @ApiOperation(value = "删除文章")
    @CacheEvict(cacheNames = "articleListByDate",allEntries = true)
    public Result<String> deleteArticle(@PathVariable Long id) throws IOException {
        log.info("删除文章：{}",id);
        articleService.deleteArticle(id);
        return Result.success("删除文章成功");
    }


    /**
     * 更新文章
     * @param articleUpdateDto
     * @return
     */
    @PutMapping("/updateArticle")
    @ApiOperation(value = "更新文章")
    public Result<String> updateArticle(@RequestBody ArticleUpdateDto articleUpdateDto) throws IOException {
        log.info("更新文章：{}",articleUpdateDto);
        articleService.updateArticle(articleUpdateDto);
        return Result.success("更新文章成功");
    }

    /**
     *  根据id获取文章
     * @param id
     * @return
     */
    @GetMapping("/getArticleById/{id}")
    @ApiOperation(value = "根据id获取文章")
    public Result<Article> getArticleById(@PathVariable Long id) {
        log.info("根据id获取文章：{}",id);
        Article article = articleService.getById(id);
        return Result.success(article);
    }


    /**
     * 根据用户id获取文章
     * @return
     */
    @GetMapping("/getArticlesByUserId/{userId}")
    @ApiOperation(value = "根据用户id分页获取文章")
    public Result<PageResult> getArticlesByUserId(@PathVariable Long userId,Integer pageNum,Integer pageSize){
        log.info("根据用户id获取文章：{}",userId);
        PageResult articlesByUserId = articleService.getArticlesByUserId(userId, pageNum, pageSize);
        return Result.success(articlesByUserId);
    }


    /**
     * 根据文章标题名获取文章
     * @param name
     * @return
     */
    @GetMapping("/getArticleByName/{name}")
    @ApiOperation(value = "根据文章标题名获取文章")
    public Result<PageResult> getArticleByName(@PathVariable String name,Integer pageNum,Integer pageSize) throws IOException {
        log.info("查询相关标题文章：{} {} {}",name,pageNum,pageSize);
        PageResult result = articleService.getArticleByTitle(name,pageNum,pageSize);
        return Result.success(result);
    }

}
